package com.macro.mall.product.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.mapper.PmPrptyMapper;
import com.macro.mall.mapper.PmPrptyPackageMapper;
import com.macro.mall.mapper.PmPrptyValMapper;
import com.macro.mall.model.*;
import com.macro.mall.product.constant.Constants;
import com.macro.mall.product.dto.PmPrptyDto;
import com.macro.mall.product.dto.PmPrptyValDto;
import com.macro.mall.product.service.PmPrptyService;
import com.macro.mall.product.util.DozerUtils;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.dozer.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class PmPrptyServiceImpl implements PmPrptyService {
    @Autowired
    private PmPrptyMapper pmPrptyMapper;
    @Autowired
    private PmPrptyValMapper pmPrptyValMapper;
    @Autowired
    private PmPrptyPackageMapper prptyPackageMapper;
    @Autowired
    private Mapper mapper;

    @Override
    public int savePmPrpty(PmPrptyDto pmPrptyDto) {
        PmPrpty pmPrpty= mapper.map(pmPrptyDto,PmPrpty.class);
        int count =0;
        if(pmPrpty.getId()!=null && pmPrpty.getId()>0){
            count= pmPrptyMapper.updateByPrimaryKey(pmPrpty);
        }else{
            pmPrpty.setStatus(Constants.STATUS_VALID);
            count= pmPrptyMapper.insert(pmPrpty);
            if(StringUtils.isBlank(pmPrpty.getPrptyCode())){
                PmPrpty pmPrptyTemp = new PmPrpty();
                pmPrptyTemp.setId(pmPrpty.getId());
                pmPrptyTemp.setPrptyCode(pmPrpty.getId().toString());
                pmPrptyMapper.updateByPrimaryKeySelective(pmPrptyTemp);
                pmPrpty.setPrptyCode(pmPrpty.getId().toString());
            }
        }
        //删除多的属性值

        PmPrptyValExample  valExample  = new PmPrptyValExample();
        PmPrptyValExample.Criteria  valCriteria = valExample.createCriteria();
        valCriteria.andPrptyIdEqualTo(pmPrpty.getId());
        valCriteria.andStatusEqualTo(Constants.STATUS_VALID);
        valExample.setOrderByClause(" prpty_seq asc ");
        List<PmPrptyVal>   prptyValList = pmPrptyValMapper.selectByExample(valExample);
        List<PmPrptyVal>   delprptyValList = new ArrayList<PmPrptyVal>();
        boolean flag;
        if(prptyValList!=null && prptyValList.size()>0){
            if(pmPrptyDto.getPrptyValList()!=null && pmPrptyDto.getPrptyValList().size()>0){
                for(PmPrptyVal val1:prptyValList){
                    flag=false;
                    for(PmPrptyValDto val2 :pmPrptyDto.getPrptyValList()){
                        if(val2.getId()!=null && val1.getId().longValue()==val2.getId().longValue()){
                            flag=true;
                            break;
                        }
                    }
                    if(!flag){
                        delprptyValList.add(val1) ;
                    }
                }
            }else{
                delprptyValList.addAll(prptyValList);
            }
        }
        if(delprptyValList!=null && delprptyValList.size()>0){
            for(PmPrptyVal val:delprptyValList)
                pmPrptyValMapper.deleteByPrimaryKey(val.getId());
        }
        log.debug("*********************** count = "+count);
        if(pmPrptyDto.getPrptyValList()!=null && pmPrptyDto.getPrptyValList().size()>0){
            PmPrptyVal pmPrptyVal = null;
            int seq=0;
            for(PmPrptyValDto valDto : pmPrptyDto.getPrptyValList()){
                pmPrptyVal =  mapper.map(valDto,PmPrptyVal.class);
                seq++;
                pmPrptyVal.setPrptySeq(seq);
                if(pmPrptyVal.getId() !=null && pmPrptyVal.getId()>0 ){
                    pmPrptyValMapper.updateByPrimaryKey(pmPrptyVal);
                }else{
                    pmPrptyVal.setPrptyId(pmPrpty.getId());
                    pmPrptyVal.setPrptyValCode(null);
                    pmPrptyVal.setStatus(Constants.STATUS_VALID);
                    pmPrptyVal.setPrptyCode(pmPrpty.getPrptyCode());
                    pmPrptyValMapper.insert(pmPrptyVal);

                    PmPrptyVal pmPrptyValTemp = new PmPrptyVal();
                    pmPrptyValTemp.setId(pmPrptyVal.getId());
                    pmPrptyValTemp.setPrptyValCode(pmPrptyVal.getId().toString());
                    pmPrptyValMapper.updateByPrimaryKeySelective(pmPrptyValTemp);
                }
            }
        }
        return count;
    }

    @Override
    public List<PmPrpty> listPrpty(int pageNum, int pageSize, PmPrptyDto param) {
        PageHelper.startPage(pageNum, pageSize);
        PmPrptyExample example  = new PmPrptyExample();
        PmPrptyExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(Constants.STATUS_VALID);
        if(!StringUtils.isBlank(param.getPrptyName())){
            criteria.andPrptyNameLike("%"+param.getPrptyName()+"%");
        }
        if(!StringUtils.isBlank(param.getPrptyCode())){
            criteria.andPrptyCodeEqualTo(param.getPrptyCode());
        }
        example.setOrderByClause(" prpty_seq asc,prpty_name asc ");
        List<PmPrpty> prptyList = pmPrptyMapper.selectByExample(example);

        return prptyList;
    }

    @Override
    public List<PmPrpty> listPrptyByPackageId(int pageNum, int pageSize, PmPrptyDto param, Long prptyPackageId) {

        PmPrpty record=  mapper.map(param,PmPrpty.class);

        String[] prptyIds=null;
        if(prptyPackageId!=null){
            PmPrptyPackage prptyPackage =  prptyPackageMapper.selectByPrimaryKey(prptyPackageId);
            if(prptyPackage!=null && !StringUtils.isBlank(prptyPackage.getPrptyIds())){
                prptyIds = prptyPackage.getPrptyIds().split(";");
            }
        }
        PageHelper.startPage(pageNum, pageSize);
        List<PmPrpty> prptyList = pmPrptyMapper.selectByRecord(record,prptyIds);

        return prptyList;
    }
    public List<PmPrptyDto> converDataList(List<PmPrpty> prptyList) {
        List<PmPrptyDto> prptyDtoList = new ArrayList<PmPrptyDto>();
        if(prptyList!=null && prptyList.size()>0){
            prptyDtoList = DozerUtils.mapList(mapper, prptyList, PmPrptyDto.class);
            if(prptyDtoList!=null && prptyDtoList.size()>0){
                PmPrptyValExample valExample  ;
                PmPrptyValExample.Criteria valCriteria ;
                List<PmPrptyVal> prptyValList = null;

                for(PmPrptyDto  prptyDto : prptyDtoList){
                    valExample  = new PmPrptyValExample();
                    valCriteria = valExample.createCriteria();
                    valCriteria.andPrptyIdEqualTo(prptyDto.getId());
                    valCriteria.andStatusEqualTo(Constants.STATUS_VALID);
                    valExample.setOrderByClause(" prpty_seq asc ");
                    prptyValList = pmPrptyValMapper.selectByExample(valExample);

                    List<PmPrptyValDto> prptyValDtoList = new ArrayList<PmPrptyValDto>();
                    if(prptyValList!=null && prptyValList.size()>0){
                        PmPrptyValDto paptyValDto = null;

                        for(PmPrptyVal prptyVal : prptyValList){
                            paptyValDto = mapper.map(prptyVal, PmPrptyValDto.class);
                            BeanUtils.copyProperties(prptyVal,paptyValDto);
                            prptyValDtoList.add(paptyValDto);
                        }
                    }
                    prptyDto.setPrptyValList(prptyValDtoList);
                }
            }
        }
        return prptyDtoList;
    }

    @Override
    public PmPrpty getById(Long id) {
        PmPrpty pmPrpty = pmPrptyMapper.selectByPrimaryKey(id);
        if(pmPrpty!=null && pmPrpty.getId()!=null){
            PmPrptyValExample valExample  ;
            PmPrptyValExample.Criteria valCriteria ;
            valExample  = new PmPrptyValExample();
            valCriteria = valExample.createCriteria();
            valCriteria.andPrptyIdEqualTo(pmPrpty.getId());
            valCriteria.andStatusEqualTo(Constants.STATUS_VALID);
            valExample.setOrderByClause(" prpty_seq asc ");
            List<PmPrptyVal>  prptyValList = pmPrptyValMapper.selectByExample(valExample);
            pmPrpty.setPrptyValList(prptyValList);
        }
        return pmPrpty;
    }
    @Override
    public PmPrpty getByCode(String  code){
        PmPrptyExample example  = new PmPrptyExample();
        PmPrptyExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(Constants.STATUS_VALID);
        criteria.andPrptyCodeEqualTo(code);
        List<PmPrpty> prptyList = pmPrptyMapper.selectByExample(example);
        if(prptyList ==null || prptyList.size()==0){
            return  null;
        }
        PmPrpty pmPrpty =prptyList.get(0);

        PmPrptyValExample valExample  ;
        PmPrptyValExample.Criteria valCriteria ;
        valExample  = new PmPrptyValExample();
        valCriteria = valExample.createCriteria();
        valCriteria.andPrptyIdEqualTo(pmPrpty.getId());
        valCriteria.andStatusEqualTo(Constants.STATUS_VALID);
        valExample.setOrderByClause(" prpty_seq asc ");
        List<PmPrptyVal>  prptyValList = pmPrptyValMapper.selectByExample(valExample);
        pmPrpty.setPrptyValList(prptyValList);

        return  pmPrpty;
    }
    @Override
    public int deletePrpty(String prptyIds){
        int count =0;
        if(!StringUtils.isBlank(prptyIds)){
            String[] prptyIdArray =prptyIds.split(";");
            for(String prptyId : prptyIdArray){
                int countInfo=pmPrptyMapper.deleteByPrimaryKey(Long.valueOf(prptyId));
                count= count+countInfo;
            }
        }
        return count;
    }

    @Override
    public int deletePrptyVal(String prptyValIds){
        int count =0;
        if(!StringUtils.isBlank(prptyValIds)){
            String[] prptyIValIdArray =prptyValIds.split(";");
            for(String prptyValId : prptyIValIdArray){
                int countInfo=pmPrptyValMapper.deleteByPrimaryKey(Long.valueOf(prptyValId));
                count= count+countInfo;
            }
        }
        return count;
    }
    public void checkPmPrpty(PmPrptyDto param) throws Exception{
        if(StringUtils.isBlank(param.getPrptyName())){
            throw new  Exception("属性名称不能为空");
        }
        PmPrptyExample example  = new PmPrptyExample();
        PmPrptyExample.Criteria criteria = example.createCriteria();
        criteria.andPrptyNameEqualTo(param.getPrptyName().trim());
        criteria.andStatusEqualTo(Constants.STATUS_VALID);

        List<PmPrpty>  prptyList =  pmPrptyMapper.selectByExample(example);
        if(prptyList!=null && prptyList.size()>0){
            if(param.getId()!=null){
                for(PmPrpty prpty:prptyList){
                    if(prpty.getId()!=param.getId()){
                        throw new  Exception("属性名称已经存在！");
                    }
                }
            }else{
                throw new  Exception("属性名称已经存在！");
            }
        }
    }
}
